<?php

namespace app\admin\controller;

use app\common\controller\AdminBase;
use think\Db;
use think\Log;


class Resourcereview extends AdminBase{
    protected function _initialize()
    {
        parent::_initialize();
        if ($this->request->isGet()) {
            $status = [
                '-1' => '已拒绝',
                '0' => '待审核',
                '1' => '已审核',
            ];
            $this->assign('status',$status);


            $code = [
                'J' => '中文论文',
                'I' => 'SCI',
                'E' => 'EI',
                'P' => '专利',
                'O' => '成果',
            ];
            $this->assign('code',$code);
        }
    }


    /**
     * 学者认领成果列表
     */
    public function index(){
        $param = $this->request->param();
        $whereOr = [];
        if (isset($param['name'])) {
            $whereOr['c.scholar_name'] = ['like', "%" . $param['name'] . "%"];
            $whereOr['c.phone'] = ['like', "%" . $param['name'] . "%"];
        }
        $where = [];
        if (isset($param['resource_code'])) {
            $where['a.resource_code'] = $param['resource_code'];
        }
        if (isset($param['status'])) {
            $where['a.status'] = $param['status'];
        }
        $list = Db::table('user_resource')->alias('a')
            ->join('user b','a.user_id = b.id')
            ->join('scholar c','b.user_no = c.kid')
            ->join('local_resource_collection d','a.resource_id = d.id')
            ->field('a.id,c.scholar_name,c.institute,c.phone,a.resource_code,d.title,a.year,d.creatorAll,a.status')
            ->whereOr(function ($query) use ($whereOr) {
                $query->whereOr($whereOr);
            })
            ->where(function ($query) use ($where) {
                $query->where($where);
            })
            //->order('a.create_time desc')
            ->paginate(config('page_number'), false, ['query' => $param]);;
        return $this->fetch('index', ['list' => $list]);
    }


    /**
     * 查看学者认领成果详情
     */
    public function resourceDetail(){
        $param = $this->request->param();
        if (isset($param['id'])) {
            $detail = Db::table('user_resource')->alias('a')
                ->join('resource_collection b','a.resource_id = b.id')
                ->select();
            return $this->fetch('resourceDetail',['detail' => $detail]);
        }
    }


    /**
     * 学者成果审核
     */
    public function resourceReview(){
        if ($this->request->isPost()) {
            $param = $this->request->param();
            if (isset($param['id']) && isset($param['status'])) {
                $updateParam = [
                    'id' => $param['id'],
                    'status' => $param['status'],
                ];
                $res = Db::table('user_resource')->update($updateParam);
                if($res > 0){
                    $this->success('审核成功');
                }else {
                    $this->error($this->errorMsg);
                }
            }
        }
        $this->error($this->errorMsg);
    }


}